import React from 'react';
import { httpConfig } from 'containers/config/httpConfig';
import { Modal,Spin,message } from 'antd';
//"dataResult":{    //此数据为打印数据dataResult必需为{} 不能是[]  data1,data2 可以随意命名但必需为[] 不能为{}
//    "data1":[{"name":"sdsds","name2":"1232322"},{"name":"sdsds","name2":"1232322"}],
//   "data2":[{"name5":"sdsds","name6":"1232322"},{"name5":"sdsds","name6":"1232322"}],  
//}
//"dataParams":{    //当数据量大不能在前端传入 此参数是直接调用后端取 dataResult         
//   "params": [{
//      "userID":"292",
//      "userCode":"1020",
//      "currentLocFlag":"Y",
//      "language":"CN"}],
//    "code":"01040043"
// }
/*
templateName:打印模板代码
hospID:医院ID
dataResult:打印的数据
let dataResult = {    //此数据为打印数据dataResult必需为{} 不能是[]  data1,data2 可以随意命名但必需为[] 不能为{}
				"data1": [{ "name": "sdsds1", "name2": "12323221" }, { "name": "sdsds1", "name2": "12323221" }],
				"data2": [{ "name5": "sdsds2", "name6": "12323222" }, { "name5": "sdsds2", "name6": "12323222" }],
			}
dataParams:后台取数据入参  当dataResult为空时   数据量比较大时可以使用
designFlag：Y:弹出编辑界面 N：不弹编辑界面
detailRowID：模板ID
logFlag:参数是否打印日志 C:\ProgramData\prykNT\prBrowser\bin\service\plugins\FastReport\Log\
*/
export async function $fastReportTemplate(templateName, hospID, dataResult, dataParams, designFlag = "N", detailRowID = "", logFlag = "N",pdfName="") {

	if (designFlag === "N") {
		let data = {
			params: [{
			}]
		}
    
		let res = await React.$asyncPost(this, '01040308', data);
		if (res.errorCode == '0') {
			if (res.designFlag == "Y") {
				/////多次打印，如果是进入编辑器会存在后面弹框覆盖前面，如果不是进行编辑器权限不会影响打印
				Modal.confirm({
					title: '报表编辑',
					content: '报表编辑，是否进入报表编辑器',
					okText: '是',
					cancelText: '否',
					onOk: () => {
						FastReportreturn(templateName, hospID, dataResult, dataParams, "Y", detailRowID, logFlag,pdfName);
					},
					onCancel: () => {
						FastReportreturn(templateName, hospID, dataResult, dataParams, "N", detailRowID, logFlag,pdfName);
					},
				});
			} else {
				return FastReportreturn(templateName, hospID, dataResult, dataParams, designFlag, detailRowID, logFlag,pdfName);
			}
		} else {
			return FastReportreturn(templateName, hospID, dataResult, dataParams, designFlag, detailRowID, logFlag,pdfName);
		}
	} else {
		return FastReportreturn(templateName, hospID, dataResult, dataParams, designFlag, detailRowID, logFlag,pdfName);
	}

}
const FastReportreturn = async (templateName, hospID, dataResult, dataParams, designFlag, detailRowID, logFlag,pdfName) => {
  let openYBModal = Modal.info({
    title: '正在打印,请稍等...',
    content: <Spin size="large" />,
    okText: "",
    onOk() { },
  });
  let { userData } = window.sessionStorage;
	let windowHost = window.location && window.location.protocol && window.location.host ? (window.location.protocol + '//' + window.location.host) : (window.location && window.location.origin ? window.location.origin : "");
	// 判断是否是本地，如果是本地的话取https配置地址，线上则取地址栏地址 （内外网）
	let httpip = windowHost && (windowHost.indexOf("localhost") !== -1 || windowHost.indexOf("127.0.0.1") !== -1) ? httpConfig.ipDeault : windowHost;
	return Promise.resolve()
		.then((oldRes) => {
			console.log('oldRes', oldRes)
			return new Promise(async (resolve, rejeck) => {
				let dllparams = {
					params: [
						{
							"designFlag": designFlag,
							"logFlag": logFlag,
							"dataResult": dataResult,
							"dataParams": dataParams,
							"pdfName":pdfName,
							"reportparams": {
								"params": [{
									fastReportCode: templateName,
									hospID: hospID,
									templateID: detailRowID,
								}],
								"code": "01040306"
							},
							"dataURL": {
								"dataIP": (httpip),
								"urlAddress": (httpConfig.urlAddress),
								"authorization": (httpConfig.authorizationToken)
							}
						}
					],
          session: [(JSON.parse(userData))]
				}
        if (typeof (CefSharp) != "undefined") { // 定制浏览器
          CefSharp.BindObjectAsync("xysCefToJavaScripts");
          return new Promise((resolve, reject) => {
              xysCefToJavaScripts.driveLocalHardware("{'inputDir':'" + "FastReport/FastReportFrom.dll" + "','clsName':'" + "FastReportFrom.FastReportDll"+ "','funcName':'" + "FastReport" + "','dllInput':'" + JSON.stringify(dllparams) + "'}").then((returnVale) => {
									var res = JSON.parse(returnVale);
									if (res.errorCode == '0') {
									}else
									{
										message.error(res.errorMessage)
									}
									resolve(returnVale)
                  openYBModal.destroy(); // 弹框等待提示
              })
          })
      }
			})
		})
}